package com.slipper.bpmn.dealer.support;

import com.slipper.constants.FlowConstants;
import com.slipper.bpmn.BpmnEngine;
import com.slipper.bpmn.dealer.INodeDealer;
import com.slipper.bpmn.vo.BpmnVO;
import com.slipper.system.flow.bo.FlowInstBO;
import com.slipper.system.flow.bo.FlowInstNodeBO;
import com.slipper.system.flow.bo.FlowNodeBO;
import com.slipper.system.flow.service.FlowInstNodeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * 并行开始节点处理器
 */
@Component
public class ParallelStartNodeDealer implements INodeDealer {
    @Resource
    private FlowInstNodeService flowInstNodeService;
    @Resource
    private BpmnEngine bpmnEngine;
    
    @Override
    public String[] getTypes() {
        return new String[] { "30" };
    }

    @Override
    public FlowInstNodeBO doDeal(
            int dealCnt,
            boolean finished,
            FlowNodeBO flowNode,
            FlowInstBO flowInst,
            FlowInstNodeBO preInstNode,
            FlowInstNodeBO currInstNode,
            BpmnVO vo) {
        if (currInstNode == null) {
            currInstNode = new FlowInstNodeBO();
            currInstNode.setInstId(flowInst.getId());
            currInstNode.setPreInstNodeId(preInstNode.getId());
            currInstNode.setLineId(flowNode.getInLineId());
            currInstNode.setCurrFlowNodeId(flowNode.getId());
            currInstNode.setDealManId(vo.getCurrEmpId());
            currInstNode.setDealOrgId(vo.getCurrOrgId());
            currInstNode.setStartTime(System.currentTimeMillis());
            currInstNode.setEndTime(System.currentTimeMillis());
            currInstNode.setUpdateTime(System.currentTimeMillis());
            currInstNode.setCreateTime(System.currentTimeMillis());
        } else {
            currInstNode = flowInstNodeService.queryBy(currInstNode.getId());
            currInstNode.setDealManId(vo.getCurrEmpId());
            currInstNode.setDealOrgId(vo.getCurrOrgId());
        }
        currInstNode.setDealStatus(FlowConstants.FlowInstNode.DEAL_STATUS_FINISHED);
        flowInstNodeService.save(currInstNode);

        int parallelCnt = bpmnEngine.next(dealCnt + 1, flowNode, flowInst, currInstNode);
        currInstNode.setParallelCnt(parallelCnt);
        flowInstNodeService.save(currInstNode);

        return currInstNode;
    }
}
